using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Service
{
    public class MetadataRTPService
    {
        static readonly object lockobj = new object();
        static MetadataRTPService instance = null;
        public static MetadataRTPService GetInstance()
        {
            if (instance == null)
            {
                lock (lockobj)
                {
                    if (instance == null)
                        instance = new MetadataRTPService();
                }
            }
            return instance;
        }
        private MetadataRTPService()
        {
        }
        public IMetaDataContent GetMetadata(string metadataId)
        {
            //return GetMetadataDemo(metadataId);
            var metadata = MetaDataClientService.GetInstance().GetMetaData(metadataId);
            if (metadata == null)
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobMetadataNotExistError, string.Format(CommonStringClassService.MetadataRTPService_GetMetadata,metadataId));
            return metadata.Content;
        }

        private IMetaDataContent GetMetadataDemo(string metadataId)
        {
            var job = new Metadata.Entity.ScheduleJob.ScheduleJob()
            {
                ID = metadataId,
                Code ="TestJob",
                Name = "TestJob",
                IsAutoRecovery =true,
                IsAllowConcurrentExecution=false,
                IsDisabled=false,
                IsPersistent =true,
                IsSysDefault=false,
                JobType = Metadata.Entity.ScheduleJob.JobType.Other,
                JobContent = "T.FAS.Runtime.ScheduleJob.PluginDemo.HelloWorld,T.FAS.Runtime.ScheduleJob.PluginDemo",
            };
            job.ScheduleTrigger = new Metadata.Entity.ScheduleJob.ScheduleTrigger()
            {
                ID = "TestJobTrigger",
                Code ="TestJobTrigger",
                Name ="TestJobTrigger",
                IsSysDefault = false,
                CronExpression = "0/5 * * * * ? ",
                TriggerType = Metadata.Entity.ScheduleJob.TriggerType.Schedule,
                MisfirePolicy = Metadata.Entity.ScheduleJob.MisfirePolicy.Schedule_DoAllMisfires
            };
            return job;
        }
    }
}
